//package com.xiguaji.tiktok.task.awemecomment.event;
//
//import com.google.common.eventbus.AllowConcurrentEvents;
//import com.google.common.eventbus.Subscribe;
//import com.gugee.tiktok.common.model.dal.blogger.BloggerLogRepository;
//import com.gugee.tiktok.common.model.dal.blogger.BloggerMainRepository;
//import com.gugee.tiktok.common.model.dal.blogger.BloggerTypeRepository;
//import com.gugee.tiktok.common.model.mysql.Aweme;
//import com.gugee.tiktok.common.model.mysql.BloggerMain;
//import com.gugee.tiktok.common.model.mysql.BloggerType;
//import com.xiguaji.boot.eventbus.Event;
//import com.xiguaji.boot.eventbus.EventBusTemplate;
//import com.xiguaji.boot.eventbus.IEventListener;
//import com.xiguaji.boot.toolkit.ThreadPoolUtils;
//import com.xiguaji.tiktok.task.aweme.AwemeService;
//import com.xiguaji.tiktok.task.ranking.client.BloggerClient;
//import com.xxl.job.core.log.XxlJobLogger;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import lombok.val;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.List;
//import java.util.concurrent.ExecutionException;
//import java.util.concurrent.ForkJoinPool;
//import java.util.concurrent.atomic.AtomicInteger;
//
//@Component
//@Slf4j
//public class RefreshTopAwemeCommentEventListener implements IEventListener<RefreshTopAwemeCommentEventListener.RefreshTopAwemeCommentEvent> {
//    @Autowired
//    BloggerLogRepository bloggerLogRepository;
//
//    @Autowired
//    BloggerMainRepository bloggerMainRepository;
//
//    @Resource(name ="BloggerClientRanking")
//    BloggerClient bloggerClient;
//
//    @Autowired
//    @Lazy
//    EventBusTemplate eventBusTemplate;
//
//    @Autowired
//    AwemeService awemeService;
//
//    List<BloggerType> bloggerTypes = new ArrayList<>();
//
//    @Autowired
//    BloggerTypeRepository bloggerTypeRepository;
//
//    @PostConstruct
//    public void initConstruct(){
//        BloggerType bloggerType = new BloggerType();
//        bloggerType.setId(0);
//        bloggerType.setName("ALL");
//        this.bloggerTypes.add(bloggerType);
//        this.bloggerTypes.addAll(bloggerTypeRepository.getBloggerTypeByIsDisplay(true));
////        scheduleTemplate.attach(topBloggerMain, forkJoinPool);
//    }
//
//
//
//    @SneakyThrows
//    @Override
//    @Subscribe
//    @AllowConcurrentEvents
//    public void handle(RefreshTopAwemeCommentEvent event) {
//
//        log.info("RefreshTopAwemeCommentEvent START typeSize:{}", bloggerTypes.size());
//        XxlJobLogger.log("RefreshTopAwemeCommentEvent START typeSize:{}", bloggerTypes.size());
//        long startTime = System.currentTimeMillis();
//        Thread single= new Thread(new SingleTask(this,event));
//        single.start();
//        try {
//            single.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//            XxlJobLogger.log(e);
//        }
//        XxlJobLogger.log("RefreshTopAwemeCommentEvent END typeSize:{} use time:{}s",bloggerTypes.size(),(System.currentTimeMillis()-startTime)/1000);
//        log.info("RefreshTopAwemeCommentEvent END typeSize:{} use time:{}s",bloggerTypes.size(),(System.currentTimeMillis()-startTime)/1000);
//
//    }
//
//    static class SingleTask implements Runnable{
//        private RefreshTopAwemeCommentEventListener topHandler;
//        private RefreshTopAwemeCommentEvent event;
//        public SingleTask(RefreshTopAwemeCommentEventListener topHandler, RefreshTopAwemeCommentEvent event){
//            this.topHandler=topHandler;
//            this.event=event;
//        }
//        @Override
//        public void run() {
//            topHandler.run(event);
//
//        }
//    }
//
//    public void run(RefreshTopAwemeCommentEvent event) {
//
//        long start = System.currentTimeMillis();
////        Semaphore semaphore=new Semaphore(1);
//        ForkJoinPool forkJoinPool = ThreadPoolUtils.forkJoinPool(300, "getBloggerRanking-work");
//        AtomicInteger allAwemeSize= new AtomicInteger();
//        AtomicInteger indx= new AtomicInteger();
//        this.bloggerTypes.forEach(t-> {
//           Thread tmp= new Thread(()->{
//               try {   List<BloggerMain> bloggerMains = bloggerClient.getBloggerRanking(t.getName().equals("ALL") ? "" : t.getName(), 1000);
//
//                    List<Aweme> awemes = Collections.synchronizedList(new ArrayList<>());
//                    forkJoinPool.submit(() ->{
//                        bloggerMains.parallelStream().forEach(bloggerMain -> {
//                                    List<Aweme> aweme = awemeService.get30DayBloggerAwemes(bloggerMain.getOUid());
//                                    awemes.addAll(aweme);
//                                });
//                            }
//                    ).get();
//                    allAwemeSize.set(awemes.size());
//                    XxlJobLogger.log("RefreshTopAwemeCommentEvent START   awemeSize:{}", awemes.size());
//                    log.info("RefreshTopAwemeCommentEvent START  awemeSize:{},queryawemetimes={}", awemes.size(),(System.currentTimeMillis()-start)/1000);
//                    if (awemes.size() > 0){
//                        val saveAwemeCommentEvent = new SaveAwemeCommentEventListener.SaveAwemeCommentEvent(event, awemes);
//                        saveAwemeCommentEvent.setType(t.getName()+":"+indx.incrementAndGet());
//                        eventBusTemplate.post(saveAwemeCommentEvent);
//                    }
//                    XxlJobLogger.log("RefreshTopAwemeCommentEvent END,type={}  awemeSize:{},user time:{}s",t.getName(),awemes.size(),(System.currentTimeMillis()-start)/1000);
//                    log.info("RefreshTopAwemeCommentEvent END ,type={},dataSize:{},user time:{}s",t.getName(),awemes.size(),(System.currentTimeMillis()-start)/1000);
//                } catch (InterruptedException | ExecutionException e) {
//                    e.printStackTrace();
//                } finally{
//
//                }
//            });
//
//            tmp.start();
//            try {
//                tmp.join();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });
//        forkJoinPool.shutdown();
//
//            XxlJobLogger.log("RefreshTopAwemeCommentEvent END  awemeSize:{},user time:{}s",allAwemeSize,(System.currentTimeMillis()-start)/1000);
//        log.info("RefreshTopAwemeCommentEvent END  awemeSize:{},user time:{}s",allAwemeSize,(System.currentTimeMillis()-start)/1000);
//
//    }
//
//    private List<BloggerMain> getBloggerMainsByType(){
//        List<BloggerMain> bloggerMains = new ArrayList<>();
//        this.bloggerTypes.forEach(b->{
//            List<BloggerMain> bloggerMainsTmp = bloggerClient.getBloggerRanking(b.getName().equals("ALL")?"":b.getName(),1000);
//            bloggerMains.addAll(bloggerMainsTmp);
//        });
//        return bloggerMains;
//    }
//
//
//    public static class RefreshTopAwemeCommentEvent extends Event {
//
//        public RefreshTopAwemeCommentEvent() {
//            super("RefreshTopAwemeCommentEvent");
//        }
//    }
//}
